import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:括号生成
 * User: wangxin
 * Date: 2024-12-26
 * Time: 21:32
 */
public class Test1 {
    public void recursion(ArrayList<String> res, StringBuffer temp, int left, int right, int n){
        if(left == n&&right == n){
            res.add(new String(temp));
        }
        if(left < n){
            recursion(res,temp.append('('),left+1,right,n);
        }
        if(right < n&&right < left){
            recursion(res,temp.append(')'),left,right+1,n);
        }
    }
    public ArrayList<String> generateParenthesis (int n) {
        // write code here
        ArrayList<String> res = new ArrayList<>();
        StringBuffer temp = new StringBuffer();
        recursion(res,temp,0,0,n);
        return res;
    }
}
